#!/bin/sh
#
# Unpack/copy a Perforce Adapter into the SNiFF installation and
# update the SiteMenus file
#
# TODO: Check for $SNIFF_DIR correctness

if [ "$SNIFF_DIR" = "" ]; then
    SNIFF_DIR="../.."
fi

if [ ! -f ./config/SiteMenus.P4.sniff ]; then
    echo "Error: your current directory is not the Perforce Adapter distribution."
    echo "Please cd to where the Perforce Adapter is located and execute ./install.sh"
    exit 1
fi

case $SNIFF_DIR in
  .*) MyDir=`pwd`
      cd $SNIFF_DIR
      SNIFF_DIR=`pwd`
      cd $MyDir
      ;;
esac

if [ ! -f $SNIFF_DIR/lib/python/Sniff/vcs.py ]; then
    echo "Error: SNIFF_DIR=$SNIFF_DIR is invalid."
    echo "Please check your SNIFF_DIR environment variable."
    exit 1
fi

if [ -f $SNIFF_DIR/config/SiteMenus.P4-orig.sniff ]; then
    echo "It seems that you already installed Perforce into your"
    echo "SNIFF_DIR=$SNIFF_DIR ."
    echo "Please remove or rename"
    echo "$SNIFF_DIR/config/SiteMenus.P4-orig.sniff"
    echo "To continue with the installation."
    exit 1
fi

echo "About to install Perforce Adapter in $SNIFF_DIR"
echo "OK [y/N] ?"
read OK
case $OK in
    y*|Y*) ;;
    *) echo "Aborting installation on user request."
       exit 1
       ;;
esac

# TODO: Site Level / User Level installation selection
echo ""
echo "Installing Perforce Adapter in $SNIFF_DIR"
cp -Rf lib $SNIFF_DIR
cp -f Preferences/VCS/Perforce.sniff $SNIFF_DIR/Preferences/VCS
if [ -f $SNIFF_DIR/config/SiteMenus.sniff ]; then
    mv $SNIFF_DIR/config/SiteMenus.sniff $SNIFF_DIR/config/SiteMenus.P4-orig.sniff
    HadSite=1
fi
cp config/SiteMenus.P4.sniff $SNIFF_DIR/config/SiteMenus.sniff

echo ""
echo "Perforce Adapter Installation is complete."
if [ "$HadSite" != "" ]; then
    echo "If you did any modifications to your site menus before installing"
    echo "Perforce, please check SNIFF_DIR/config/SiteMenus.P4-orig.sniff"
    echo "to merge your personal modifications."
fi
